#include "ce_ui_sprite.h"
#include "../renderer/ce_ui_renderer.h"

enum CE_UISpriteFlag
{
    NONE = 0x00,
    VERTEX = 0x01,
    SHADER = 0x02,
    COLOR = 0x04,
    ALL = 0xFF,
};

CE_UISprite::CE_UISprite()
{
    std::cout << "CE_UISprite constructor" << std::endl;
    this->_flag = CE_UISpriteFlag::ALL;
    this->_x = 0;
    this->_y = 0;
    this->_width = 100;
    this->_height = 100;
    this->_anchorX = 0.5;
    this->_anchorY = 0.5;
    this->_color[0] = 1;
    this->_color[1] = 1;
    this->_color[2] = 1;
    this->_color[3] = 1;
    this->_shaderPath = "resources/effects/default/default";
    this->_renderer = new CE_UIRenderer();
    this->update();
}
void CE_UISprite::setPosition(float x, float y)
{
    this->_x = x;
    this->_y = y;
    this->_flag |= CE_UISpriteFlag::VERTEX;
}
void CE_UISprite::setSize(float width, float height)
{
    this->_width = width;
    this->_height = height;
    this->_flag |= CE_UISpriteFlag::VERTEX;
}
void CE_UISprite::setAnchor(float x, float y)
{
    this->_anchorX = x;
    this->_anchorY = y;
    this->_flag |= CE_UISpriteFlag::VERTEX;
}
void CE_UISprite::setColor(float r, float g, float b, float a)
{
    this->_color[0] = r;
    this->_color[1] = g;
    this->_color[2] = b;
    this->_color[3] = a;
    this->_flag |= CE_UISpriteFlag::COLOR;
}

void CE_UISprite::setShader(std::string shaderPath)
{
    this->_shaderPath = shaderPath;
    this->_flag |= CE_UISpriteFlag::SHADER;
}

void CE_UISprite::update()
{   

    // 先更新渲染状态
    if (this->_flag & CE_UISpriteFlag::SHADER)
    {
        this->_renderer->updateShader(this->_shaderPath);
    }
    // 再更新顶点数据
    if (this->_flag & CE_UISpriteFlag::VERTEX)
    {

        this->_renderer->updateVertexs(this->_x, this->_y, this->_anchorX, this->_anchorY, this->_width, this->_height);
    }
    // 最后更新颜色数据
    if (this->_flag & CE_UISpriteFlag::COLOR)
    {
        this->_renderer->updateColor(this->_color[0], this->_color[1], this->_color[2], this->_color[3]);
    }
    // 最后清除标志位
    this->_flag = CE_UISpriteFlag::NONE;
}

CE_UISprite::~CE_UISprite()
{
}